package com.cl.ks.controller;

import com.cl.ks.service.KsRuleServiceOld;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author chenyitao
 * @date 2021/4/20
 */
@RestController
@RequestMapping("/rule")
@CrossOrigin
@Api(tags = "规则接口")
public class KsRuleControllerOld {
    private final KsRuleServiceOld ksRuleServiceOld;

    public KsRuleControllerOld(KsRuleServiceOld ksRuleServiceOld) {
        this.ksRuleServiceOld = ksRuleServiceOld;
    }

    @ApiOperation("获取规则运算符")
    @RequestMapping(path = "/ops", method = RequestMethod.GET)
    public ResponseEntity<Object> getOps() {
        return ksRuleServiceOld.getOps();
    }

    @ApiOperation("获取规则函数")
    @RequestMapping(path = "func", method = RequestMethod.GET)
    public ResponseEntity<Object> getFunc() {
        return ksRuleServiceOld.getFunc();
    }

    @ApiOperation("新增规则")
    @RequestMapping(path = "/add", method = RequestMethod.POST)
    public ResponseEntity<Object> addKsRule(@RequestHeader(name = "Authorization", defaultValue = "") String auth, @RequestBody KsRuleServiceOld.KsRuleParam ksRuleParam) {
        return ksRuleServiceOld.addKsRule(auth, ksRuleParam);
    }

    @ApiOperation("编辑规则")
    @RequestMapping(path = "/edit", method = RequestMethod.POST)
    public ResponseEntity<Object> editKsRule(@RequestHeader(name = "Authorization", defaultValue = "") String auth, @RequestBody KsRuleServiceOld.KsRuleParam ksRuleParam) {
        return ksRuleServiceOld.editKsRule(auth, ksRuleParam);
    }

    @ApiOperation("删除规则")
    @RequestMapping(path = "/del", method = RequestMethod.GET)
    public ResponseEntity<Object> delKsRule(@RequestHeader(name = "Authorization", defaultValue = "") String auth, @RequestParam(name = "id") Integer id) {
        return ksRuleServiceOld.delKsRule(auth, id);
    }
    @ApiOperation("查询规则列表")
    @RequestMapping(path = "/getKsRuleList", method = RequestMethod.POST)
    public ResponseEntity<Object> getKsRuleListNew(@RequestHeader(name = "Authorization", defaultValue = "") String auth, @RequestBody Map<String, Object> param) {
        return ksRuleServiceOld.getKsRuleListNew(auth, param);
    }
    @ApiOperation("查询规则列表")
    @RequestMapping(path = "/list", method = RequestMethod.POST)
    public ResponseEntity<Object> getKsRuleList(@RequestHeader(name = "Authorization", defaultValue = "") String auth, @RequestBody Map<String, Object> param) {
        return ksRuleServiceOld.getKsRuleList(auth, param);
    }

    @ApiOperation("单条数据判断字段是否符合规则需求")
    @RequestMapping(path = "/testFilterData", method = RequestMethod.POST)
    public ResponseEntity<Object> testFilterData(@RequestBody KsRuleServiceOld.FilterDataParam filterDataParam) {
        return ksRuleServiceOld.testFilterData(filterDataParam);
    }
    @ApiOperation("单条数据判断是否符合规则")
    @RequestMapping(path = "/filterData", method = RequestMethod.POST)
    public ResponseEntity<Object> filterData(@RequestBody KsRuleServiceOld.FilterDataParam filterDataParam) {
        return ksRuleServiceOld.filterData(filterDataParam);
    }

    @ApiOperation("批量判断数据是否符合规则")
    @RequestMapping(path = "/batchFilterData", method = RequestMethod.POST)
    public ResponseEntity<Object> batchFilterData(@RequestBody List<KsRuleServiceOld.FilterDataParam> filterDataParams) {
        List<Object> res = new ArrayList<>();
        filterDataParams.forEach(i -> {
            res.add(ksRuleServiceOld.filterData(i).getBody());
        });
        return ResponseEntity.ok(res);
    }

    @ApiOperation("批量判断数据是否符合规则")
    @RequestMapping(path = "/batchFilterDataByTableName", method = RequestMethod.POST)
    public ResponseEntity<Object> batchFilterDataByTableName(@RequestBody List<KsRuleServiceOld.FilterDataParam> filterDataParams) {
        List<Object> res = new ArrayList<>();
        filterDataParams.forEach(i -> {
            res.add(ksRuleServiceOld.filterData(i).getBody());
        });
        return ResponseEntity.ok(res);
    }

}
